<template>
	<view class="layout">
		<!-- 壁纸预览层 -->
		<swiper :current="currentIndex" circular @change="swiperChange" v-if="currentInfo">
			<swiper-item v-for="(item,index) in classList" :key="item._id">
				<image v-if="readImgs.includes(index)" @click="imageMask" :src="item.picurl" mode="aspectFill"></image>
			</swiper-item>
		</swiper>
		<!-- 遮罩层 -->
		<view class="mask" v-if="maskState">
			<!-- #ifndef MP-TOUTIAO -->
				<view class="goback" @click="goBack" :style="{top:getStatusBarHeight()+'px',marginLeft:getLeftIconLeft()+'px'}">
					<uni-icons type="left" size="20" color="#fff"></uni-icons>
				</view>
			<!-- #endif -->
			<view class="count">{{currentIndex+1}} / {{classList.length}}</view>
			<view class="time">
				<uni-dateformat :date="Date.now()" format="hh:mm"></uni-dateformat>
			</view>
			<view class="date">
				<uni-dateformat :date="Date.now()" format="MM月dd日"></uni-dateformat>
			</view>
			<view class="footer" v-if="currentInfo">
				<view class="box" @click="clickInfo">
					<uni-icons type="info" size="28"></uni-icons>
					<view class="text">信息</view>
				</view>
				<view class="box" @click="clickScore">
					<uni-icons type="star" size="28"></uni-icons>
					<view class="text">{{currentInfo.score}}分</view>
				</view>
				<view class="box" @click="clickDownload">
					<uni-icons type="download" size="23"></uni-icons>
					<view class="text">下载</view>
				</view>
			</view>
		</view>
		<!-- 壁纸信息弹出窗口 -->
		<uni-popup ref="infoPopup" type="bottom">
			<view class="infoPopup">
				<view class="popHeader">
					<view></view>
					<view class="tile">壁纸信息</view>
					<view class="close" @click="clickInfoClose">
						<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
					</view>
				</view>
				<scroll-view scroll-y >
					<view class="content" v-if="currentInfo">
						<view class="row">
							<view class="label">壁纸ID:</view>
							<text selectable class="value">{{currentInfo._id}}</text>
						</view>
						<!-- <view class="row">
							<view class="label">分类:</view>
							<text class="value class"></text>
						</view> -->
						<view class="row">
							<view class="label">发布者:</view>
							<text class="value">{{currentInfo.nickname}}</text>
						</view>
						<view class="row">
							<view class="label">评分:</view>
							<view class="value">
								<uni-rate v-model="value" :value="currentInfo.score" size="16" readonly touchable></uni-rate>
								<text class="score">{{currentInfo.score}}分</text>
							</view>
						</view>
						<view class="row">
							<view class="label">摘要:</view>
							<text class="value">{{currentInfo.description}}</text>
						</view>	
						<view class="row">
							<view class="label">标签:</view>
							<view class="value tabs">
								<view class="tab" v-for="(item,index) in currentInfo.tabs" :key="index">{{item}}</view>
							</view>
						</view>
						<view class="copyRight">
							声明：本图片来自用户投稿，非商业用途，用于免费教学使用，如侵犯了您的权益，您可拷贝壁纸ID举报至平台客服，管理将删除壁纸，维护您的权益
						</view>
						<view class="safe-area-inset-bottom"></view>
					</view>
				</scroll-view>
			</view>
		</uni-popup>
		<!-- 壁纸评分弹出窗口 -->
		<uni-popup ref="scorePopup" :is-mask-click="false">
			<view class="scorePopup">
				<view class="popHeader">
					<view></view>
					<view class="tile">{{isScore ? '评分过了~':'壁纸评分'}}</view>
					<view class="close" @click="clickScoreClose">
						<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
					</view>
				</view>
				<view class="content" v-if="currentInfo">
					<uni-rate allowHalf v-model="userScore" :disabled="isScore" disabled-color="#ffca3e"></uni-rate>
					<text class="text">{{userScore}}分</text>
				</view>
				<view class="footer">
					<button @click="submitScore" :disabled="isScore" type="default" size="mini" plain>确认评分</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>


<script setup>
	import { goHome } from '../../utils/common';
	import { getStatusBarHeight,getLeftIconLeft } from '../../utils/system_info'
	import {apiGetSetupScore,apiWriteDownload,apiGetWallPaperDetail} from "@/api/apis.js"
	
	const classList=ref([])
	//获取缓存数据
	const storageClassList=uni.getStorageSync("storageClassList") || []
	//清洗数据
	classList.value=storageClassList.map(item=>{
		return {
			...item,
			picurl:item.smallPicurl.replace("_small.webp",".jpg")
		}
	})
	
	//遮罩层状态
	const maskState=ref(true)
	//信息弹窗对象
	const infoPopup=ref(null)
	//评分弹窗对象
	const scorePopup=ref(null)
	//用户评分
	const userScore=ref(0)
	//用户是否评分
	const isScore=ref(false)
	//看过的图
	const readImgs=ref([])
	//点击预览的id
	const currentId=ref(null)
	//点击预览的下标
	const currentIndex=ref(0)
	//当前图片信息
	const currentInfo=ref(null)
	
	
	//加载页面时接收参数
	onLoad(async (e)=>{
		currentId.value=e.id
		if(e.type=='share'){
			let res=await apiGetWallPaperDetail({
				id:e.id
			})
			classList.value=res.data.map(item=>{
				return {
					...item,
					picurl:item.smallPicurl.replace("_small.webp",".jpg")	
				}
			})
		}
		if(classList.value.length==0)goHome()
		currentIndex.value=classList.value.findIndex(item=>currentId.value==item._id)
		currentInfo.value=classList.value[currentIndex.value]
		readImgsFun()
	})
	
	//滑动事件
	function swiperChange(e){
		currentIndex.value=e.detail.current
		currentInfo.value=classList.value[currentIndex.value]
		readImgsFun()
	}
	
	//readImgs加载函数
	function readImgsFun(){
		readImgs.value.push(
			currentIndex.value<=0 ? classList.value.length-1:currentIndex.value-1,
			currentIndex.value,	
			currentIndex.value+1>=classList.value.length-1 ? 0:currentIndex.value+1
		)
		//去重
		readImgs.value=[...new Set(readImgs.value)]
	}
	
	//点击之后显示/隐藏遮罩层mask
	function imageMask(){
		maskState.value=!maskState.value
	}
	
	//点击返回上一页
	function goBack(){
		uni.navigateBack({
			fail:(e)=>{
				uni.reLaunch({
					url:"/pages/index/index"
				})
			}
		})
	}
	
	//点击信息弹窗显示
	function clickInfo(){
		infoPopup.value.open()
	}
	//点击关闭信息弹窗
	function clickInfoClose(){
		infoPopup.value.close()
	}
	
	//点击评分弹出窗口
	function clickScore(){
		scorePopup.value.open()
		//如果用户已经评分,就将显示用户的评分
		if(currentInfo.value.userScore){
			isScore.value=true	
			userScore.value=currentInfo.value.userScore
		}
	}
	
	//点击关闭评分窗口
	function clickScoreClose(){
		scorePopup.value.close()
		userScore.value=0
		isScore.value=false
	}
	
	//提交评分
	async function submitScore(){
		uni.showLoading({
			title:"提交中..."
		})
		let {classid,_id:wallId}=currentInfo.value
		let res=await apiGetSetupScore({
			classid,
			wallId,
			userScore:userScore.value
		})
		uni.hideLoading()
		//如果评分成功，提示
		if(res.errCode==0){
			uni.showToast({
				title:"评分成功"
			})
			//手动通过本地缓存修改数据源
			classList.value[currentIndex.value].userScore=userScore.value
			uni.setStorageSync("storageClassList",classList.value)
			//关闭弹窗
			clickScoreClose()
		}
	}
	
	
	//点击下载
	async function clickDownload(){
		// #ifdef H5
			uni.showModal({
				content:"请长按保存壁纸",
				showCancel:false
			})
		// #endif
		// #ifndef H5
			try{
				uni.showLoading({
					title:"正在保存...",
					mask:true
				})

				let {classid,_id:wallId}=currentInfo.value
				let res=await apiWriteDownload({
					classid,
					wallId
				})
				if(res.errCode!=0)throw res
			
				uni.getImageInfo({
					src:currentInfo.value.picurl,
					success:(res)=>{
						uni.saveImageToPhotosAlbum({
							filePath:res.path,
							success:(res)=>{
								uni.showToast({
									title:"保存成功"
								})
							},
							fail:(err)=>{
								if(err.errMsg=='saveImageToPhotoAlbum:fail cancel'){
									uni.showToast({
										title:"保存失败，请重新下载",
										icon:"none"
									})
									return
								}
								uni.showModal({
									title:"授权提示",
									content:"需要授权保存到相册",
									success:(res)=>{
										if(res.confirm){
											uni.openSetting({
												success:(setting)=>{
													if(setting.authSetting["scope.writePhotosAlbum"]){
														uni.showToast({
															title:"授权成功",
															icon:"none"
														})
													}else{	
														uni.showToast({
															title:"授权失败",
															icon:"none"
														})
													}
												}
											})
										}
									}
								})
							},
							complete:()=>{
								uni.hideLoading()
							}
						})
					}
				})
			}catch{
				uni.hideLoading()
			}
		// #endif
	}
	
	//分享给好友
	onShareAppMessage((e)=>{
		return {
			title:'贤贤虾壁纸预览',
			path:'/pages/classlist/classlist?id='+currentId.value+"&type=share"
		}
	})
	
	//分享到朋友圈
	onShareTimeline(()=>{
		return {
			title:'贤贤虾壁纸预览~',
			query:'id='+currentId+"&type=share"
		}
	})
</script>

<style lang="scss" scoped>
	.layout{
		width: 100%;
		height: 100vh;
		//图片预览
		swiper{
			width: 100%;
			height: 100%;
			swiper-item{
				width: 100%;
				height: 100%;
				image{
					width: 100%;
					height: 100%;
				}
			}
		}
		//遮罩层
		.mask{
			&>view{
				position: absolute;
				left: 0;
				right: 0;
				margin:auto;
				width: fit-content;
				color: white;
			}
			.goback{
				display: flex;
				align-items: center;
				justify-content: center;
				top: 0%;
				left: 30rpx;
				margin-left: 0;
				width: 38px;
				height: 38px;
				border-radius: 100px;
				background-color: rgba(0,0,0,0.5);
				backdrop-filter: blur(10rpx);
			}
			.count{
				top: 10vh;
				padding: 8rpx 28rpx;
				text-align: center;
				font-size: 28rpx;
				background-color: rgba(0, 0, 0, 0.3);
				border-radius: 40rpx;
				backdrop-filter: blur(10rpx);
			}
			.time{
				top: calc(10vh + 80rpx);
				font-size: 140rpx;
				font-weight: 100;
				line-height: 1;
				text-shadow: 0 4rpx rgba(0,0,0,0.3);
			}
			.date{
				top: calc(10vh + 230rpx);
				text-shadow: 0 2rpx rgba(0,0,0,0.3);
				font-size: 34rpx;
			}
			.footer{
				display: flex;
				justify-content: space-around;
				align-items: center;
				bottom: 10vh;
				width: 65vw;
				height: 120rpx;
				border-radius: 120rpx;
				color: $text-font-color1;
				background-color: rgba(255,255,255,0.8);
				box-shadow: 0 2rpx 0 rgba(0,0,0,0.1);
				backdrop-filter: blur(20rpx);
				.box{
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					padding: 2rpx 12rpx;
					// border: 1rpx solid red;
					.text{
						font-size: 26rpx;
						color: $text-font-color2;
					}
				}
			} 
		}
		//弹窗公用头部样式
		.popHeader{
			display: flex;
			justify-content: space-between;
			align-items: center;
			.title{
				color: $text-font-color2;
				font-size: 226rpx;
			}
			.close{
				padding: 6rpx;
			}
		}
		//壁纸信息弹窗
		.infoPopup{
			padding: 30rpx;
			border-radius: 30rpx 30rpx 0 0;
			background-color: #fff;
			overflow: hidden;
			scroll-view{
				max-height: 50vh;
				.content{
					.row{
						display: flex;
						padding: 16rpx;
						font-size: 32rpx;
						line-height: 1.7em;
						.label{
							width: 140rpx;
							text-align: right;
							color: $text-font-color3;
							font-size: 30rpx;
						}
						.value{
							display: flex;
							align-items: center;
							flex: 1;
							width: 0;
							.score{
								padding: 10rpx;
								font-size: 26rpx;
								color: $text-font-color2;
							}
						}
						.tabs{
							display: flex;
							flex-wrap: wrap;
							.tab{
								margin: 0 10rpx 10rpx 0;
								padding: 10rpx 30rpx;
								border: 1px solid $brand-theme-color;
								color: $brand-theme-color;
								font-size: 22rpx;
								border-radius: 40rpx;
								line-height: 1em;
							}
						}
						.class{
							color:$brand-theme-color;
						}
					}
					.copyRight{
						margin: 20rpx 0;
						padding: 20rpx;
						background-color: #f6f6f6;
						font-size: 28rpx;
						line-height: 1.6em;
						border-radius: 10rpx;
						color: #666;
					}
				}
			}
		}
		// 评分弹窗
		.scorePopup{
			padding: 30rpx;
			width: 70vw;
			border-radius: 30rpx;
			background-color: #fff;
			.content{
				display: flex;
				justify-content: center;
				align-items: center;
				padding: 30rpx 0;
				.text{
					padding-left: 10rpx;
					width: 80rpx;
					line-height: 1em;
					text-align: right;
					white-space: nowrap;
					color: #ffca3e;
				}	
			}
			.footer{
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 10rpx 0;
			}
		}
	}
</style>
